<?php
/**
 * Notes:
 * User:ZSLAA
 * DateTime: 2020/11/5 10:23
 */


namespace app\admin\controller;


use app\admin\Base\BaseController;
use think\facade\Request;
use think\facade\View;
use app\admin\model\Tags as TagsModel;

class Tags extends BaseController
{
    //标签列表页面
    public function index(){
        if (Request::isAjax()) {
            $list = TagsModel::order('id','desc')->paginate(Request::get('limit'));
            $this->jsonApi('', 1, $list->items(), ['count' => $list->total(), 'limit' => Request::get('limit')]);
        }
        return View::fetch();
    }

    //标签删除
    public function del(){
        $article = TagsModel::find(Request::post('id'));
        $status = $article->delete();

        if ($status){
            $this->jsonApi('删除成功',1);
        }

        $this->jsonApi('删除失败',0);
    }

    //标签批量删除
    public function delAll(){
        $ids = Request::post('ids');
        if (empty($ids)){
            $this->jsonApi('未选数据',0);
        }
        $idsArr = explode(',',$ids);
        $status = TagsModel::destroy($idsArr);
        if ($status){
            $this->jsonApi('批量删除数据成功',1);
        }
        $this->jsonApi('批量删除数据失败',0);
    }

    //标签添加
    public function add(){
        if (Request::isAjax()) {
            //获取登入参数
            $data = Request::param();
            //保存到数据库
            $articleCategory = new TagsModel();
            $status = $articleCategory->save($data);
            if ($status){
                return $this->jsonApi('添加成功',1);
            }
            return $this->jsonApi('添加失败',0);
        }
        return View::fetch();
    }

    //标签编辑
    public function edit(){

    }

    //是否启用
    public function status(){
        $data = Request::post();
        $article = TagsModel::update(['status' => $data['status'], 'id' => $data['id']]);
        if (empty($article)){
            $this->jsonApi("设置失败",0);
        }
        $this->jsonApi("修改成功",1);
    }

}